package com.tiles.controller;

import com.tiles.entity.TilesUser;
import com.tiles.service.TilesUserService;
import com.tiles.tools.MsgUtil;
import com.tiles.tools.SmsCode;
import com.tiles.tools.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/ajax")
public class AjaxController {
    @Autowired
    private TilesUserService service;
    //    验证码
    @RequestMapping(value = "/a1",method = RequestMethod.POST)
    @ResponseBody
    public MsgUtil setSmsCode(String phone,HttpServletResponse response) {
        String code = SmsCode.RandomCode();
        Jedis jedis = new Jedis();
        jedis.setex(phone, 180, code);
        jedis.close();
        MsgUtil msgUtil = new MsgUtil();
        msgUtil.msg = "验证码已发送";
        return msgUtil;

    }
    @RequestMapping(value = "/a2", method = RequestMethod.POST)
    @ResponseBody
    public MsgUtil testAjax2(String tilesUserName, String password, String email, String phone, String smsCode ,HttpServletResponse response) throws IOException {
        System.out.println("=================================");
        MsgUtil msg = new MsgUtil();
        if (tilesUserName != null) {
            int i = service.selectByUserName(tilesUserName);
            if (i == 1) {
                msg.code = -2;
                msg.msg = "用户名已存在";
            } else {
                msg.msg = "可注册";
                if (password != null) {
                    if (password.length() >= 7 && password.length() < 20) {
                        if (email != null) {
                                if (phone != null) {
                                    Jedis jedis = new Jedis();
                                   String s=jedis.get(phone);
                                    System.out.println(s);
                                        if (smsCode.equals(s)) {
                                            TilesUser tilesUser = new TilesUser();
                                            tilesUser.setTilesUsername(tilesUserName);
                                            tilesUser.setTilesPassword(password);
                                            tilesUser.setEmail(email);
                                            tilesUser.setPhone(phone);
                                            System.out.println(tilesUser);
                                            try {
                                                service.insertTilesUser(tilesUser);
                                                msg.code = 0;
                                                msg.msg = "注册成功";
                                                 } catch (Exception e) {
                                                    e.printStackTrace();
                                            }

                                        }else {
                                        msg.msg = "验证码不正确";
                                    }
                                }
                            }else {
                                msg.msg = "邮箱已注册";
                            }
                    }else {
                        msg.msg = "密码不符合要求";
                    }
                }
            }
        } else {
            msg.code = -1;
            msg.msg = "用户名不能为空";

        }

        return msg;
    }

}
